//
//  GetAreaCode.m
//  easyTaxi
//
//  Created by ellenpage on 12-11-29.
//  Copyright (c) 2012年 ellenpage. All rights reserved.
//

#import "GetAreaCode.h"
#import "FMDatabase.h"
#import "PassValueClass.h"
#import "FMDatabaseAdditions.h"

@implementation GetAreaCode

+ (NSString *)getAreaCode:(NSString *)provice
{    
    FMDatabase *db;
    NSString   *areaCodeStr;
    NSString   *twoFirstCode;
    NSString   *fourFirstCode;
    
    //dbPath： 数据库路径，在Document中。
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"xzqh.db"];
    db = [FMDatabase databaseWithPath:dbPath];
    if (![db open])
    {
        NSLog(@"Could not open db.");
    }
    
    if ([provice isEqualToString:@"北京市"]||[provice isEqualToString:@"上海市"]||[provice isEqualToString:@"重庆市"]||[provice isEqualToString:@"天津市"])
    {
        areaCodeStr = [db stringForQuery:@"SELECT CODE FROM XZQH WHERE NAME like ?",[PassValueClass sharedIntance].districtStr];
    }
    else
    {
        NSString *codeHereProvince = [db stringForQuery:@"SELECT CODE FROM XZQH WHERE NAME like ?",[PassValueClass sharedIntance].provinceStr];
        twoFirstCode = [codeHereProvince substringToIndex:2];
        if ([[PassValueClass sharedIntance].cityStr length] != 0 && [twoFirstCode length] != 0)
        {
            NSString *sqlForCheckCity = [NSString stringWithFormat:@"SELECT * FROM XZQH WHERE CODE like '%@\%%' and NAME like '%@'",twoFirstCode,[PassValueClass sharedIntance].cityStr];
            NSString *codeHereCity = [db stringForQuery:sqlForCheckCity];
           fourFirstCode = [codeHereCity substringToIndex:4];
        }
        if ([[PassValueClass sharedIntance].districtStr length] != 0 && [fourFirstCode length] != 0)
        {
            NSString *sqlForCheckDistrict = [NSString stringWithFormat:@"SELECT * FROM XZQH WHERE CODE like '%@\%%' and NAME like '%@'",fourFirstCode,[PassValueClass sharedIntance].districtStr];
            areaCodeStr = [db stringForQuery:sqlForCheckDistrict];
        }
    }
    return areaCodeStr;
}

@end
